// pages/login/login.js
const {
    loginWx,
    loginAccount
} = require('../../services/loginReq')
const {
    getMyFamily
} = require("../../services/myFamilyReq")
const app = require('../../app')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        username: '',
        password: '',
        from: ''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let pages = getCurrentPages()
        if (pages.length >= 2) {
            let prevpage = pages[pages.length - 2]
            this.setData({
                from: prevpage.route
            })
        } else {
            this.setData({
                from: 'pages/mine/mine'
            })
        }
    },

    // 用户名输入
    usernameChange(e) {
        this.setData({
            username: e.detail
        })
    },

    // 密码输入
    passwordChange(e) {
        this.setData({
            password: e.detail
        })
    },

    // 跳转注册
    handleGoRegister() {
        wx.navigateTo({
            url: '../registerPersonal/registerPersonal',
        })
    },

    // 账号密码登录
    handleLogin(e) {
        if (this.data.username === '' || this.data.password === '') {
            wx.showToast({
                title: '信息不完整',
                icon: 'none'
            })
            return
        }
        wx.showLoading({
            title: '登录中',
            mask: true
        })
        loginAccount(this.data.username, this.data.password, 'volunteer', res => {
            console.log(res)
            if (res.data.code === 0) {
                wx.setStorageSync('token', res.data.token)

                wx.setStorageSync('volunteerInfo', res.data.info)
                wx.showToast({
                    title: '登录成功',
                    icon: 'success'
                })
                getMyFamily(res => {
                    wx.setStorageSync('familyInfo', res.data.data)
                })
                if (
                    this.data.from === 'pages/mine/mine' ||
                    this.data.from === 'pages/regesterPersonal/regesterPersonal'
                ) {
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                } else {
                    wx.navigateBack({
                        delta: 1,
                    })
                }
            } else {
                wx.showToast({
                    title: '登录失败',
                    icon: 'none'
                })
            }
        })
    },

    // 微信快捷登录
    handleLoginWx(e) {
        wx.showLoading({
            title: '登录中'
        })

        wx.login({
            success: res => {
                console.log(res)
                loginWx(res.code, 'volunteer', res => {
                    // console.log(res)
                    if (res.data.code === 0) {
                        wx.setStorageSync('token', res.data.token)
                        wx.setStorageSync('volunteerInfo', res.data.info)
                        wx.showToast({
                            title: '登录成功',
                            icon: 'success'
                        })
                        if (this.data.from === 'pages/mine/mine') {
                            wx.reLaunch({
                                url: '../index/index',
                            })
                        } else {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }
                        //  登陆时获取家庭信息
                        getMyFamily(res => {
                            wx.setStorageSync('familyInfo', res.data.data)
                        })

                    } else {
                        wx.showToast({
                            title: '快捷登录失败',
                            icon: 'error'
                        })
                        return wx.navigateTo({
                            url: '../registerPersonal/registerPersonal'
                        })
                    }
                })
            },
            fail: res => {
                // console.log(res)
            }
        })
    }
})